//
//  ProfileInfoViewController.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/5/14.
//

import UIKit

class ProfileInfoViewController: CommonViewController {
    private var headerImageView:UIImageView!
    private var delta :CGFloat = 0.0
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = BACKGROUND_COLOR
        self.navigationItem.leftBarButtonItem?.tintColor = .white
        var items:Array<UIBarButtonItem> = []
        let sels = [#selector(moreMenu),#selector(search),#selector(userInfo)]
        let images = ["morevertical","search","profileinfo"]
        for i in 0...2{
            let image = UIImage(named: images[i])?.withRenderingMode(.alwaysTemplate).reSizeImage(reSize: CGSize.init(width: 20, height: 20))
            let item = UIBarButtonItem.init(image: image, style: .plain, target: self, action: sels[i])
            item.tintColor = .white
            items.append(item)
        }
        self.navigationItem.rightBarButtonItems = items
        let tableView = UITableView.init(frame: .zero, style: .grouped)
        view.addSubview(tableView)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.tableFooterView = UIView()
        tableView.separatorColor = Separator_COLOR
        tableView.backgroundColor = .white
        headerImageView = UIImageView()
        headerImageView.backgroundColor = .red
        headerImageView.image = UIImage(named: "AppIcon")
        headerImageView.contentMode = .scaleAspectFill
        
        tableView.snp.makeConstraints({
            $0.top.left.bottom.right.equalTo(0)
        })
        let tempView = UIView()
        tempView.frame = CGRect.init(x: 0, y: 0, width: ScreenWidth, height: 200)
        tempView.addSubview(headerImageView)
        headerImageView.frame = tempView.bounds
        tableView.tableHeaderView = tempView
        self.navigationController?.navigationBar.isHidden = false
        self.navigationController?.modalPresentationCapturesStatusBarAppearance = false
        self.statusBarStyle = .lightContent
    }
    
    @objc func userInfo(){
        self.statusBarStyle = .lightContent
    }
    @objc func search(){
        
    }
    @objc func moreMenu(){
        self.statusBarStyle = .default
        self.setNeedsStatusBarAppearanceUpdate()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.setBackgroundImage(UIColor.init(red:1, green: 1, blue: 1, alpha: delta).toImage(), for: UIBarMetrics.default)
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
}
extension ProfileInfoViewController:UITableViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offesty = scrollView.contentOffset.y + CGFloat(navigationBarHeight)
        if offesty < 0 {
            headerImageView.frame = CGRect.init(x: (ScreenWidth-(ScreenWidth - offesty/2))/2, y: 0, width: ScreenWidth - offesty/2, height: 200 - offesty/2)
            SystemLog(headerImageView.frame)
        }
        delta = scrollView.contentOffset.y / CGFloat(navigationBarHeight) + 1
        delta = CGFloat.maximum(delta, 0)
        let b:CGFloat = 0.6
        self.statusBarStyle = delta > b  ? .default:.lightContent
        self.navigationItem.leftBarButtonItem?.tintColor = delta > b  ? B000_COLOR:.white
        for item in navigationItem.rightBarButtonItems! {
            item.tintColor = delta > b  ? B000_COLOR:.white
        }
        self.navigationController?.navigationBar.setBackgroundImage(UIColor.init(red:1, green: 1, blue: 1, alpha: delta).toImage(), for: UIBarMetrics.default)
    }
}
extension ProfileInfoViewController:UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 40
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    
}
